package leeon.bbsbrower.views;


import leeon.bbsbrower.util.ImageDownloadManager;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressIndicator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ProgressMonitorDialog extends Dialog {

	protected ProgressMonitorDialog(Shell parentShell) {
		super(parentShell);
		
	}
	
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	protected Control createDialogArea(Composite parent) {
		
		ImageDownloadManager manager = ImageDownloadManager.newInstance();
		
		int index = -1;
		while ((index = manager.needUI()) != -1) {
		
	        Label messageLabel = new Label(parent, SWT.NONE);
	        messageLabel.setText("");
	        GridData data = new GridData(GridData.GRAB_HORIZONTAL
	                | GridData.HORIZONTAL_ALIGN_FILL
	                | GridData.VERTICAL_ALIGN_BEGINNING);
	        data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
	        messageLabel.setLayoutData(data);
	        
	        ProgressIndicator progressIndicator = new ProgressIndicator(parent);
	        GridData gd = new GridData();
	        gd.heightHint = convertVerticalDLUsToPixels(9);
	        gd.horizontalAlignment = GridData.FILL;
	        gd.grabExcessHorizontalSpace = true;
	        gd.horizontalSpan = 2;
	        progressIndicator.setLayoutData(gd);
	        
	        manager.setUI(index, progressIndicator, messageLabel);
		}
		
		Label messageLabel = new Label(parent, SWT.NONE);
        messageLabel.setText("");
        GridData data = new GridData(GridData.GRAB_HORIZONTAL
                | GridData.HORIZONTAL_ALIGN_FILL
                | GridData.VERTICAL_ALIGN_BEGINNING);
        data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
        messageLabel.setLayoutData(data);
        
        manager.setLabel(messageLabel);
        
		return parent;
	}
	
	 /**
     * 设定窗口的标题和图片
     */
    protected void configureShell(Shell shell) {
    	super.configureShell(shell);
    	shell.setText("下载进度");    	
    }
		
	/**
	 * 去掉下面的button bar
	 */
	protected Control createButtonBar(Composite parent) {
		return null;
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	protected Point getInitialSize() {
		return new Point(500, 300);
	}

	protected void handleShellCloseEvent() {
		super.handleShellCloseEvent();
		ImageDownloadManager manager = ImageDownloadManager.newInstance();
        manager.removeAllUI();
        manager.removeLabel();
	}
}
